package com.codeark.appengine.objectify;

import java.util.Date;

import org.joda.time.DateTime;

import com.googlecode.objectify.impl.conv.Converter;
import com.googlecode.objectify.impl.conv.ConverterLoadContext;
import com.googlecode.objectify.impl.conv.ConverterSaveContext;

/**
 * 
 * @author Yaniv Kessler (yaniv at codeark dot com)
 *
 */
public class JodaDateTimeConverter implements Converter {

	@Override
	public Object forDatastore(Object value, ConverterSaveContext ctx) {
		if (DateTime.class.isAssignableFrom(value.getClass())) {
			DateTime dateTime = (DateTime) value;
			return dateTime.toDate();
		}
		
		return null;
	}

	@Override
	public Object forPojo(Object value, Class<?> fieldType, ConverterLoadContext ctx, Object onPojo) {
		if (Date.class.isAssignableFrom(value.getClass())) {
			Date date = (Date) value;
			return new DateTime(date);
		}
		return null;
	}

	

}
